﻿var count = 1; //上传组件个数
$(function() {
    //添加上传组件
    $("#btnAdd").click(function() {
        if ($("#DivUploads").find(":button").length >= 10) {
            alert('最多只能添加10个上传组件！');
            return;
        }
        var strHtml = '<span><input type="file" name="fileUpload" runat="server" />';
        strHtml += " <input type='button' onclick='delUploadBtn(" + count + ")' value='删除'/></span>";
        $("#DivUploads").append(strHtml);
        count++;
    });
});
//删除上传组件
function delUploadBtn(index) {
    $("#DivUploads").find(":button").each(function() {
        var text = "" + $(this).attr("onclick");
        if (text.indexOf("delUploadBtn(" + index + ")") != -1) {
            $(this).parent().remove();
        }
    });
}

function checkupload() {
    var obj = document.getElementById('FileUpload1');
    if (obj.value == '') {
        alert("请上传文件!");
        return false;
    }
    var filelist = document.getElementsByName("FileUpload");
    if (filelist.length > 0) {
        if (!checkAllFileList(filelist)) {
            return false;
        }
    }
    else {
        if (!checkAllFile(obj)) {
            return false;
        }
    }
    return true;
}
function checkAllFile(file) {
    var fileContentType = file.value.match(/^(.*)(\.)(.{1,8})$/)[3];
    if (fileContentType == 'gif' || fileContentType == 'jpeg' || fileContentType == 'png' || fileContentType == 'jpg' || fileContentType == 'docx'
        || fileContentType == 'doc' || fileContentType == 'xlsx' || fileContentType == 'xls' || fileContentType == 'rar') { }
    else {
        alert("上传文件格式不正确，目前不支持格式为：" + fileContentType);
        return false;
    }
    return true;
}
function checkAllFileList(filelist) {
    //验证是否为空
    for (var i = 0; i < filelist.length; i++) {
        if (filelist[i].value == "") {
            alert("上传文件不能为空！");
            return false;
        }
    }
    //验证是否是合法的文件
    for (var i = 0; i < filelist.length; i++) {
        var fileContentType = filelist[i].value.match(/^(.*)(\.)(.{1,8})$/)[3];
        if (fileContentType == 'gif' || fileContentType == 'jpeg' || fileContentType == 'png' || fileContentType == 'jpg' || fileContentType == 'docx'
        || fileContentType == 'doc' || fileContentType == 'xlsx' || fileContentType == 'xls' || fileContentType == 'rar') {
        } else {
            alert("上传文件格式不正确，目前不支持格式为：" + fileContentType);
            return false;
        }
    }
    //验证是否有重复上传内容
    for (var i = 0; i < filelist.length; i++) {
        for (var j = filelist.length - 1; j > i; j--) {
            if (filelist[j].value == filelist[i].value) {
                alert("重复数据:" + filelist[i].value + "，请重新添加!");
                return false;
            }
        }
    }
    return true;
}